package dataClass;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;



public class ProductInJson extends JSONObject{
	public static String ID = "id";
	public static String TYPE = "type";
	public static String NAME = "name";
	public static String PRODUCER = "producer";
	public static String PRICE = "price";
	public static String RESERVE = "number";
	public static String SENDTIME = "time";
	
	public static String[] NAMES = { ID, TYPE, NAME, PRODUCER, PRICE, RESERVE, SENDTIME };
	
	private static String PRODUCER_VALUE = "BUYME";
	
	private Product goods;
	
	public static String handleString (String s) {
		if (s == null) 
			return "";
		return s;
	}
	
	private static int getDays (Date d) {
		if (d == null)
			return 1;
		Date now = new Date();
		long minus = (((d.getTime() - now.getTime())/1000)/3600)/24;
		if (minus <= 0)
			return 1;
		return (int)minus;
	}
	
	public ProductInJson () {
		goods = new Product();
	}
	
	public ProductInJson (Product goods) {
		if (this.goods == null)
			this.goods = new Product();
		if (goods == null)
			return;
		setGoods (goods);
	}
	
	public ProductInJson (String s) throws JSONException {
		super(s);
		goods = new Product();
		this.setID(this.getInt("id"));
		this.setName(this.getString("name"));
		this.setPrice(this.getDouble("price"));
		this.setProducer(this.getString("producer"));
		this.setType(this.getString("type"));
		this.setReserve(this.getInt("number"));
	}
	
	public Product getGoods () {
		return goods;
	}
	
	public void setGoods (Product goods) {
		this.goods = goods;
		int id = goods.ID;
		String type = handleString (goods.type);
		String name = handleString (goods.name);
		String producer = handleString (goods.producer);
		double price = goods.outprice;
		int number = goods.quantity;
//		int time = getDays(goods);
		
		put(ID, id);
		put (TYPE, type);
		put (NAME, name);
		put (PRODUCER, producer);
		put (RESERVE, number);
		put (PRICE, price);
//		put (SENDTIME, time);
	}
	/**
	 * 
	 * @return category's name
	 */
	public String getType () {
		return goods.type;
	}
	public int getID() {
		return goods.ID;
	}
//	public Category getCategory () {
//		return goods.getCategory();
//	}
	
	public String getName () {
		return goods.getName();
	}
	
	public double getPrice () {
		return goods.outprice;
	}
	
//	public int getReserve () {
//		return goods.getReserve();
//	}
	/**
	 * 
	 * @return sendtime with Date type
	 */
//	public Date getSendtime () {
//		return goods.getSendtime();
//	}
	/**
	 * 
	 * @return sendtime with integer type
	 */
	public int getTime () {
		return (Integer)get(SENDTIME);
	}
	
	public String getProducer() {
		return goods.producer;
	}
	
	public void setType (String type) {
		type = handleString (type);
		goods.type = type;
		put (TYPE, type);
	}
	public void setID (int id) {
		goods.ID = id;
		put (ID, id);
	}
	
//	public void setCategory (Category c) {
//		c.setName(handleString(c.getName()));
//		goods.setCategory(c);
//		put (TYPE, c.getName());
//	}
	
	public void setName (String name) {
		name = handleString(name);
		goods.setName(name);
		put (NAME, name);
	}
	
	public void setProducer (String producer) {
		producer = handleString(producer);
		goods.producer = producer;
		put (PRODUCER, producer);
	}
	
	public void setPrice (double price) {
		goods.outprice = (float)price;
		put (PRICE, price);
	}
	
//	public void setSendtime (Date sendtime) {
//		goods.setSendtime(sendtime);
//		put (SENDTIME, getDays(sendtime));
//	}
//	
	public void setReserve (int reserve) {
		goods.quantity = reserve;
		put (RESERVE, reserve);
	}
	
}
